package com.tmanager.web.servlets;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
import com.tmanager.persistence.dao.ActivityDAO;
import com.tmanager.persistence.dao.UserDAO;
import com.tmanager.persistence.entity.Activity;
import com.tmanager.persistence.entity.ActivityType;
import com.tmanager.persistence.entity.User;
import com.tmanager.persistence.tx.Transactional;
import com.tmanager.web.services.AddedActivityService;

public class ActivityServlet extends InjectedHttpServlet {
	private static final Logger log = LoggerFactory.getLogger(ActivityServlet.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = -2379120062564396234L;

	static final String REQUEST_ACTIVITY_JSON = "data";

	

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		request.setCharacterEncoding("UTF-8");
		String data = request.getParameter(REQUEST_ACTIVITY_JSON);
		log.debug(data);
		data = new String(data.getBytes(), "UTF8");

		try {
			JSONObject json = new JSONObject(data);
			getInjector().getInstance(AddedActivityService.class).saveActivity(json);
		} catch (JSONException e) {
			log.error(e.getMessage(), e);
		}

		// BufferedReader in = new BufferedReader(new
		// InputStreamReader(request.getInputStream()));
		// String line = null;
		// while ((line = in.readLine()) != null) {
		// log.debug(line);
		// }
	}
}
